package leaf.science.pages
{
	import caurina.transitions.Tweener;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import leaf.science.nav.Page;

	public class PageIntro extends Page
	{
		private var anim:LeafIntroAnimation;
		
		public function PageIntro()
		{
			setupSkin();
			super();
		}
		
		override public function goIn():void
		{
			Tweener.removeTweens(anim.white_mc);
			Tweener.removeTweens(anim.black_mc);
			Tweener.removeTweens(anim.title_mc);
			Tweener.removeTweens(anim.skip_mc);
			Tweener.addTween(anim.white_mc, {scaleY:1, time:0.6, transition:"easeOutQuadric", delay:0.2});
			Tweener.addTween(anim.black_mc, {scaleY:1, time:0.6, transition:"easeOutQuadric", delay:0.4, onComplete:playVideo});
			Tweener.addTween(anim.title_mc, {alpha:1, time:1.2, transition:"easeInOutCubic", delay:0.8});
			Tweener.addTween(anim.skip_mc, {alpha:1, time:0.8, transition:"easeInOutCubic", delay:1.2});
		}
		
		private function playVideo ():void
		{
			anim.player_cmp.visible = true;
			anim.player_cmp.play();
		}
		
		override public function goOut():void
		{
			anim.player_cmp.stop();
			anim.player_cmp.visible = false;
			Tweener.removeTweens(anim.white_mc);
			Tweener.removeTweens(anim.black_mc);
			Tweener.removeTweens(anim.title_mc);
			Tweener.removeTweens(anim.skip_mc);
			Tweener.addTween(anim.skip_mc, {alpha:0, time:0.3, transition:"easeInOutCubic", delay:0});
			Tweener.addTween(anim.title_mc, {alpha:0, time:0.3, transition:"easeOutCubic", delay:0});
			Tweener.addTween(anim.black_mc, {scaleY:0, time:0.3, transition:"easeOutQuadric", delay:0.2});
			Tweener.addTween(anim.white_mc, {scaleY:0, time:0.3, transition:"easeOutQuadric", delay:0.4, onComplete:onOut});
		}
		
		private function onOut():void
		{
			visible = false;
			setTimeout(onOutComplete, 50);
		}
		
		override public function toString():String
		{
			return 'intro';
		}
		
		
		private function setupSkin():void
		{
			anim = new LeafIntroAnimation();
			addChild(anim);
		}
		
		
		
		private function skip(evt:Event = null):void
		{
			trace('skip');
			anim.player_cmp.stop();
			anim.player_cmp.seek(0);
			LeafScience.instance.setAddress('demos');
		}
		
		override public function onResize(evt:Event=null):void
		{
			super.onResize();
			anim.skip_mc.x = stage.stageWidth/2 - 5;
			anim.title_mc.y = int((-190 -y + 44)/2);
		}
		
		override protected function onAddedToStage(evt:Event):void
		{
			anim.player_cmp.visible = false;
			anim.white_mc.scaleY = 0;
			anim.black_mc.scaleY = 0;
			anim.title_mc.alpha = 0;
			
			anim.skip_mc.stop();
			anim.skip_mc.buttonMode = true;
			anim.skip_mc.mouseChildren = false;
			anim.skip_mc.hitArea = anim.skip_mc.hit_mc;
			anim.skip_mc.alpha = 0;
			anim.skip_mc.addEventListener(MouseEvent.CLICK, skip);
			
			anim.player_cmp.source = '../video/intro.flv';
			anim.player_cmp.addEventListener('complete', skip);
			onResize();
			super.onAddedToStage(evt);
		}
		
	}
}